package com.hc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.hc.domain.Offer;
import com.hc.domain.OfferSpec;
import com.hc.service.IOfferService;
import com.hc.service.IOfferSpecService;
import com.hc.util.FileUtils;
import com.hc.util.IdWorker;
import com.hc.util.LoggerUtil;
import com.hc.util.OtherUtils;

@Controller
@RequestMapping(value="Offer")
public class OfferController extends BaseController {
	
	@Resource
	private IOfferSpecService offerSpecService;
	@Resource
	private IOfferService offerService;
	/**
	 * 
	 * @Description： 获取分类
	 * @return
	 * @author:  
	 * @since: 2017年8月29日 上午11:31:17
	 */
	@ResponseBody
	@RequestMapping(value="getSpec",method=RequestMethod.GET)
	public JSON getSpec() {
		List<OfferSpec> spec = offerSpecService.findBySort();
		return Message(SUCCESS, "获取分类成功", spec);
	}
	
	/**
	 * 
	 * @Description： 获取分类
	 * @return
	 * @author:  
	 * @since: 2017年8月29日 上午11:31:17
	 */
	@ResponseBody
	@RequestMapping(value="getOffers",method=RequestMethod.POST)
	public JSON getOffers(@RequestParam(value="id",required=true)int id) {
		List<Offer> offers = offerService.findByCateId(id);
		return Message(SUCCESS, "获取分类成功", offers);
	}
	/**
	 * 
	 * @Description： 保存
	 * @param req
	 * @param resp
	 * @param id
	 * @param name
	 * @param price
	 * @param file
	 * @return
	 * @author:  
	 * @since: 2017年8月29日 下午2:19:15
	 */
	@ResponseBody
	@RequestMapping(value="saveOffers",method=RequestMethod.POST)
	public JSON saveOffers(HttpServletRequest req,HttpServletResponse resp,
			@RequestParam(value = "id", required = true) int id,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "price", required = true) String price,
			@RequestParam(value = "file", required = false) MultipartFile file) {
			if (id==1) {
				if (file==null||file.isEmpty()) {
					return Message(ERROR, "请上传车辆信息图片", null);
				}
			}
			BigDecimal pr = null;
			try {
				pr = new BigDecimal(price);
				pr = pr.setScale(2, BigDecimal.ROUND_HALF_UP);
			} catch (Exception e) {
				return LoggerUtil.logInfo(e, "请输入合法的数字,保留两位小数");
			}
			String urlPath = "";
			if (file!=null&&!file.isEmpty()) {
				String newFileName = IdWorker.createId();
				String ext = "gif,jpg,jpeg,png,bmp";
				String path = req.getContextPath();
				String basePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + path + "/";
				FileOutputStream fos = null;
				InputStream in = null;
				File f = new File(FILE_PATH + "Offer/");
				if (!f.exists()) {
					f.mkdirs();
				}
				try {
					fos = new FileOutputStream(FILE_PATH + "Offer/" + newFileName);
					in = file.getInputStream();
					String fileExt = FileUtils.getFileTypeByStream(IOUtils.toByteArray(file.getInputStream()));
					if (isEmpty(fileExt)||ext.indexOf(fileExt)<0) {
						return Message(ERROR, "未知图片类型（合法格式:PNG/JPEG/JPG/GIF/BMP）", null);
					}
					int b = 0;
					while ((b = in.read()) != -1) {
						fos.write(b);
					}
					urlPath = basePath + "File/getFile/" + "Offer/" + newFileName;
				} catch (Exception e) {
					return LoggerUtil.logInfo(e, "服务器异常，请联系管理员");
				} finally {
					try {
						if (fos != null) {
							fos.close();
						}
						if (in != null) {
							in.close();
						}
					} catch (Exception e2) {
						LoggerUtil.error(e2);
					}
				}
			}
			Offer o = new Offer();
			o.setExt("");
			o.setImg(urlPath);
			o.setName(name);
			o.setOfferSpecId(id);
			o.setPrice(pr);
			o.setStatus(1);
			offerService.save(o);
		return Message(SUCCESS, "保存成功", o);
	}
	/**
	 * 
	 * @Description： 删除
	 * @param id
	 * @return
	 * @author:  
	 * @since: 2017年8月29日 下午3:36:59
	 */
	@ResponseBody
	@RequestMapping(value="del",method=RequestMethod.POST)
	public JSON del(@RequestParam(value="id",required=true)int id) {
		Offer o = offerService.getById(id);
		if (o==null) {
			return Message(ERROR, "该分类不存在", null);
		}
		o.setStatus(99);
		offerService.update(o);
		return Message(SUCCESS, "删除成功", null);
	}
}

